package com.android.qjpmc.manager;

import static android.content.Context.LOCATION_SERVICE;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.android.qjpmc.widget.manager.RxJavaHttpManager;
import com.android.qjpmc.widget.manager.retrofit.RetrofitJsonManager;
import com.android.qjpmc.widget.permissions.PermissionListener;
import com.android.qjpmc.widget.permissions.PermissionsUtil;
import com.android.qjpmc.widget.retrofithelper.rxsubscriber.CommonObserver;
import com.orhanobut.logger.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class LocationAMapManager {
    // 声明AMapLocationClient类对象
    private AMapLocationClient locationClient;
    // 声明AMapLocationClientOption对象
    private AMapLocationClientOption locationOption;

    private static class LocationLocalManagerSingleton {
        private static final LocationAMapManager INSTANCE = new LocationAMapManager();

    }

    public static LocationAMapManager getInstance() {
        return LocationAMapManager.LocationLocalManagerSingleton.INSTANCE;
    }

    private LocationAMapManager() {
    }

    public void initLocation(Context context, AMapLocationListener locationListener) {
        AMapLocationClient.updatePrivacyAgree(context, true);
        AMapLocationClient.updatePrivacyShow(context, true, true);
        String[] permissions = {
                Manifest.permission.ACCESS_FINE_LOCATION,

        };
        PermissionsUtil.requestPermission(context, new PermissionListener() {
                    @Override
                    public void permissionGranted(@NonNull String[] permissions) {
                        initLocationManager(context, locationListener);
                    }

                    @Override
                    public void permissionDenied(@NonNull String[] permissions) {
                        //  ToastUtils.showShort("请到设置-权限管理中开启");
                    }
                }, permissions, false, null
        );


    }

    private void initLocationManager(Context context, AMapLocationListener locationListener) {
        // 初始化定位
        try {
            locationClient = new AMapLocationClient(context);
            locationOption = new AMapLocationClientOption();

            // 设置定位模式为高精度模式
            locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);


            // 设置定位参数
            locationOption.setSensorEnable(true);
            locationOption.setInterval(2000);
            locationOption.setOnceLocation(true);
            locationClient.setLocationOption(locationOption);
            // 设置定位监听
            locationClient.setLocationListener(locationListener);
            // 启动定位
            locationClient.startLocation();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
